//
//  ConsultPostViewController.m
//  homeland
//
//  Created by caesar on 14-8-3.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "ConsultPostViewController.h"
#import "CommonUtil.h"
#import "ConsultListViewController.h"
#import "ListViewController.h"
#define INPUT_VIEW_OFFSET 200.0

@interface ConsultPostViewController ()

@end

@implementation ConsultPostViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self initwith];
}

//初始化控件
-(void)initwith{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title = @"咨询";
    self.navigationItem.rightBarButtonItem = rightNavBtnWithImgName(@"咨询记录", self, @selector(rightButtonAction:));
    
    UserInfoService *userService = [UserInfoService getUserInfoService];
    if (userService.userInfo) {
        if (userService.userInfo.mobile) {
            self.phoneField.text = userService.userInfo.mobile;
        }
        
        if (userService.userInfo.userName) {
            self.nameField.text = userService.userInfo.userName;
        }
    }
    
    _nameField.inputAccessoryView = [self getInputAccessoryView];
    _nameField.delegate = self;
    
    _phoneField.inputAccessoryView = [self getInputAccessoryView];
    _phoneField.delegate = self;
    
    _contentField.inputAccessoryView = [self getInputAccessoryView];
    _contentField.delegate = self;
    
    
    _contentField.layer.borderColor = [CommonUtil stringToColor:@"#C0C0C0"].CGColor;
    _contentField.layer.borderWidth =1.0;
    _contentField.layer.cornerRadius =5.0;

    
    [self.view addSubview: self.myView];
    SEL selector = @selector(postBtnEvent);
    UIButton *postBtn = [CommonUtil createBottonBtn:@"btnImage.png" btnName:@"提交" selector:selector delegate:self];
    [self.view addSubview:postBtn];

}
//提交事件
-(void)postBtnEvent{
    NSString *phoneText = self.phoneField.text;
    NSString *contentText = self.contentField.text;
    NSString *titleText = self.titleField.text;
    NSString *nameText = self.nameField.text;
    
    if (!titleText || titleText.length == 0) {
        [self showAlertWithTitle:@"提示" Msg:@"咨询事项不能为空"];
        [_titleField becomeFirstResponder];
        return;
    }
    if (!contentText || contentText.length == 0) {
        [self showAlertWithTitle:@"提示" Msg:@"咨询内容不能为空"];
        [_contentField becomeFirstResponder];
        return;
    }
    if (!phoneText || phoneText.length == 0) {
        [self showAlertWithTitle:@"提示" Msg:@"电话不能为空"];
        [_phoneField becomeFirstResponder];
        return;
    }
    if (!nameText || nameText.length == 0) {
        [self showAlertWithTitle:@"提示" Msg:@"姓名不能为空"];
        [_nameField becomeFirstResponder];
        return;
    }
    [self posstConsult];
}
//提交记录
#pragma mark - Right Button
-(void)rightButtonAction:(id)sender
{
    ListViewController *cVc = [[ListViewController alloc]initWithNibName:@"ListViewController" bundle:nil];
    
    cVc.config = @{@"key":@"社区网格员-咨询列表",@"uid": [self.config objectForKey:@"uid"]};
    [self.navigationController pushViewController:cVc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Text Field Delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.2f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y - INPUT_VIEW_OFFSET;
    self.view.frame = frame;
    [UIView commitAnimations];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.2f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y + INPUT_VIEW_OFFSET;
    self.view.frame = frame;
    [UIView commitAnimations];
}

#pragma mark - Text View Delegate
//开始输入的时候向上移动整个界面，这个输入框不会被挡住
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.2f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y - INPUT_VIEW_OFFSET/3;
    self.view.frame = frame;
    [UIView commitAnimations];
}

//输入结束的时候把整个界面向下移动，这个能恢复正常
-(void)textViewDidEndEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.2f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y + INPUT_VIEW_OFFSET/3;
    self.view.frame = frame;
    [UIView commitAnimations];
}

- (void)posstConsult
{
    [self startLoadingViewWithText:@"正在提交..."];
    UserInfo *userInfo = [UserInfoService getUserInfoService].userInfo;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[self.config objectForKey:@"uid"] forKey:@"uid"];
    [params setValue:_titleField.text forKey:@"title"];
    [params setValue:_phoneField.text forKey:@"phone"];
    [params setValue:_nameField.text forKey:@"name"];
    [params setValue:_contentField.text forKey:@"content"];
    [params setValue:userInfo.Id forKey:@"userId"];
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:SERVER_URL_COMMUNITY_WAITER_CONSULT_ADD  customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:@"" params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        
        [self stopLoadingView];
        NSDictionary *json = [completedOperation responseJSON];
        BOOL *isSuccess = [[json objectForKey:@"isSuccess"]boolValue];
        if (isSuccess) {
            [self showAlertWithTitle:@"提示" Msg:@"提交成功，等待处理"];
        }
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        [self stopLoadingView];
        [self.view makeToast:@"请求失败" duration:2.0 position:@"center"];
    }];
    [currentEngine enqueueOperation:op];
}

@end
